package com.lintcode.solution;

public class Solution_54 {
    public static void main(String[] args) {
        //        System.out.println(atoi("    +1111"));
        //        System.out.println(atoi("    51sdfa"));
        System.out.println(atoi("-2147483649"));
    }
    
    public static int atoi(String str) {
        if (str == null || "".equals(str)) {
            return 0;
        }
        Character temp;
        long result = 0;
        char[] chars = str.trim().toCharArray();
        boolean flag = false;
        for (int i = 0; i < chars.length; i++) {
            char c = chars[ i ];
            if (i == 0 && c == '-') {
                flag = true;
                continue;
            }
            if (i == 0 && c == '+') {
                continue;
            }
            if (c == '.') {
                break;
            }
            if (! (c >= '0' && c <= '9')) {
                break;
            }
            temp = c;
            int a = Integer.parseInt(temp.toString());
            result = result * 10 + a;
            if (result * 10 >= Integer.MAX_VALUE) {
                return flag ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
        }
        return (int) (flag ? (- 1 * result) : result);
    }
}
